﻿Public Class Bewerkklant
    Dim customers As New Customers
    Dim users As New Users
    Dim LoginService As New LoginService
    Dim incidents As New Incidents
    Dim userid As Integer
    Dim userpassword As String
    Dim nieuwegebruiker As Boolean = False
    Public Function weergeefKlant(ByVal uid As String, Optional ByVal nieuw As Boolean = False) 'nieuw = als er een nieuwe gebruiker wordt gemaakt.

        If nieuw = False Then
            customers.LoadFromDB(uid)
            Voorletter_TB.Text = customers.voorletter
            Naam_TB.Text = customers.achternaam
            Ovnummer_TB.Text = customers.ovnummer
            'Geblokkeerd_CB.Checked = customers.disabled
            userpassword = customers.wachtwoord
            Email_TB.Text = customers.email
            Telefoonnr_TB.Text = customers.telefoon
            Email_TB.Text = customers.email
            userid = customers.Id
            'incidenten weergeven
            If incidents.LoadAllFromDB(customers.ovnummer) = "True" Then
                Dim inbehandelingcount As Integer = 0
                Dim opencount As Integer = 0
                Dim geslotencount As Integer = 0
                For Each row As DataRow In incidents.IncidentsDataSet.Tables("Table").Rows()
                    IncidentenListview.Items.Add(row.Item("id")).ToString()
                    IncidentenListview.Items(IncidentenListview.Items.Count - 1).SubItems.Add(row.Item("apparaat")).ToString()
                    IncidentenListview.Items(IncidentenListview.Items.Count - 1).SubItems.Add(row.Item("categorie")).ToString()
                    IncidentenListview.Items(IncidentenListview.Items.Count - 1).SubItems.Add(row.Item("status")).ToString()
                    If (row.Item("status").ToString() = "Inbehandeling") Then
                        inbehandelingcount = inbehandelingcount + 1
                    ElseIf (row.Item("status").ToString() = "Gesloten") Then
                        geslotencount = geslotencount + 1
                    ElseIf (row.Item("status").ToString() = "Geopend") Then
                        opencount = opencount + 1
                    End If
                Next
                Incidentencount_Label.Text = "Incidenten: " + opencount.ToString + " open, " + inbehandelingcount.ToString + " in behandeling, " + geslotencount.ToString + " gesloten."
            End If
        Else
            'Nieuwe klant
            Me.Size = New Point(332, 368)
            CheckBox1.Enabled = False
            CheckBox1.Checked = True
            nieuwegebruiker = nieuw
            RemoveUser_PB.Visible = False
        End If

        Me.Show()
        Return True
    End Function


    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Afbreken_PB.Click
        Me.Close()
    End Sub
    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveUser_PB.Click
        Dim msg As MsgBoxResult = _
        MsgBox("Weet je zeker dat je " + customers.voorletter + " " + customers.achternaam + " wilt verwijderen? Alle incidenten van de klant worden ook verwijderd!", MsgBoxStyle.YesNo, "Verwijderen: " + users.ovnummer)

        Dim localdataset = New DataSet
        If msg = MsgBoxResult.Yes Then
            Dim result As String = customers.DeleteFromDB(userid, True)
            If Not result = "True" Then
                MsgBox("er is iets fout gegaan bij het verwijderen van de incidenten: " + result + vbNewLine + "De gebruiker is niet verwijderd.")
            End If

            AdminmenuV2.refreshCurrentList()
            Me.Close()
        End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged, Geblokkeerd_CB.CheckedChanged
        Nieuwpw_TB.Enabled = CheckBox1.Checked
        Nieuwpw2_TB.Enabled = CheckBox1.Checked
        Nieuwpw_TB.Text = ""
        Nieuwpw2_TB.Text = ""
    End Sub

    Private Sub Opslaan_PB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Opslaan_PB.Click
        If Not IsNumeric(Telefoonnr_TB.Text) Then
            MsgBox("Het telefoonnummer mag alleen uit nummers bestaan.")
            Exit Sub
        End If
        If Nieuwpw_TB.Text = Nieuwpw2_TB.Text Then
            userpassword = Nieuwpw_TB.Text

            customers.clearData()
            customers._Id = userid
            customers.achternaam = Naam_TB.Text
            customers.voorletter = Voorletter_TB.Text
            customers.ovnummer = Ovnummer_TB.Text
            customers.email = Email_TB.Text
            customers.telefoon = Telefoonnr_TB.Text
            'customers.disabled = Geblokkeerd_CB.Checked
            customers.wachtwoord = LoginService.MD5(userpassword)
            customers.SaveToDB(userid, nieuwegebruiker)
            AdminmenuV2.refreshCurrentList()
            Me.Close()
        Else
            MsgBox("Het wachtwoord komt niet overeen.")
        End If
    End Sub

    Private Sub IncidentenListview_Doubleclick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IncidentenListview.DoubleClick
        If IncidentenListview.SelectedItems.Count.ToString >= 1 Then
            Bewerkincident.weergeefIncident(IncidentenListview.SelectedItems.Item(0).SubItems.Item(0).Text.ToString)
        End If
    End Sub

    Private Sub Bewerkklant_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class